// Server.h

/*
=============================================
  Source Revision Date : 2007-02-09
  Source Revision By : KyungTaek, Lim
  Update Comments :

  (2007-01/31)
  	1. 소켓 객체 모델 붙임.
  (2007-02-09)
  	1. Optimizer 추가
	2. 클라이언트 요청시 동작 수정 및 추가
  (2007-02-12)
	1. RSS Link를 DB에서 주기적으로 Pumping하는 스레드 추가.

=============================================
*/

#ifndef _SERVER_H
#define _SERVER_H

#include "UserMGR.h"
#include "ChannelMGR.h"
#include "ItemMGR.h"
#include "TagMGR.h"
#include "RSSDocumentLinkMGR.h"
#include "RSSRefresher.h"
#include "ClientMGR.h"
#include "Optimizer.h"

         
//전역 스레드 함수 /////////////////////////////////////////////////////////////////////
void* TF_ClntConnection( void* arg_ );
void* TF_RSSRefresher( void* arg_ );
void* TF_LinkLoader( void* arg_ );

pthread_mutex_t     socket_mutx;
pthread_mutex_t     allMgr_mutx;


//클래스 : 서버 선언 ///////////////////////////////////////////////////////////////////
class Server
{
public :	// Default Function ////////////////////////////////////////////////////////
	Server ();
	Server ( int port,
			 UserMGR* userMGR, ChannelMGR* channelMGR, ItemMGR* itemMGR, TagMGR* tagMGR, 
			 RSSDocumentLinkMGR* rssDocumentLinkMGR, RSSRefresher* rssRefresher, ClientMGR* clientMGR, Optimizer* optimizer);
	Server( const Server& source );
	~Server();
	
	Server& operator = ( const Server& source );
	bool operator	== ( const Server& other );
	bool operator	!= ( const Server& other );
	
	
public:		//General Function//////////////////////////////////////////////////////////
	
	//Start Thread Function
	void Transfer();
	void Refresher();
	void LinkLoader();
	
	// RSS Generate Function
	string& XmlGenerate( string filename, int itemCount, Item* itemSet, Tag* keyWord );	

	
public:		// Access Function /////////////////////////////////////////////////////////
	
	UserMGR* 			GetUserMGR() 			const;
	ChannelMGR* 		GetChannelMGR() 		const;
	ItemMGR* 			GetItemMGR() 			const;
	TagMGR* 			GetTagMGR() 			const;
	RSSDocumentLinkMGR* GetRSSDocumentLinkMGR() const;
	RSSRefresher* 		GetRSSRefresher() 		const;
	ClientMGR* 			GetClientMGR() 			const;
// 	pthread_mutex_t& 	GetSocketMutx()			const;
//	pthread_mutex_t&	GetAllMGRMutx()			const;
	PSocket* 			GetServerSocket()		const;
	Optimizer*			GetOptimizer()			const;
	
private :	//Members
	
	int 				port;
	PSocket* 			serverSocket;

	// Manager Members
	ChannelMGR* 		channelMGR;
	ItemMGR* 			itemMGR;
	TagMGR* 			tagMGR;
	UserMGR* 			userMGR;
	RSSDocumentLinkMGR* rssDocumentLinkMGR;
	RSSRefresher* 		rssRefresher;
	ClientMGR* 			clientMGR;
	Optimizer*			optimizer;
	
};	// Class Define End!


// Structure for Thread Function // Important! /////////////////////////////////////////
typedef struct _arg
{
	Server*				thisServer;
	Client* 			thisClient;
    int 				thisSock;

	ChannelMGR*         channelMGR;
	ItemMGR*            itemMGR;
	TagMGR*             tagMGR;
	UserMGR*            userMGR;
	RSSDocumentLinkMGR* rssDocumentLinkMGR;
	RSSRefresher*       rssRefresher;
	Optimizer*          optimizer;
	
} Arg;




// Inline Functions ///////////////////////////////////////////////////////////////////
inline PSocket* Server::GetServerSocket() const
{
    return const_cast<PSocket*>( this->serverSocket );
}

inline UserMGR* Server::GetUserMGR() const
{
	return const_cast<UserMGR*>( this->userMGR );
}

inline ChannelMGR* Server::GetChannelMGR() const
{
	return const_cast<ChannelMGR*>( this->channelMGR );
}

inline ItemMGR* Server::GetItemMGR() const
{
	return const_cast<ItemMGR*>( this->itemMGR );
}

inline TagMGR* Server::GetTagMGR() const
{
	return const_cast<TagMGR*>( this->tagMGR );
}

inline RSSDocumentLinkMGR* Server::GetRSSDocumentLinkMGR() const 
{
	return const_cast<RSSDocumentLinkMGR*>( this->rssDocumentLinkMGR );
}

inline RSSRefresher* Server::GetRSSRefresher() const
{
	return const_cast<RSSRefresher*>( this->rssRefresher );
}

inline ClientMGR* Server::GetClientMGR() const
{
	return const_cast<ClientMGR*>( this->clientMGR );
}

inline Optimizer* Server::GetOptimizer() const
{
	return const_cast<Optimizer*>( this->optimizer );
}



#endif // _SERVER_H
